<template>
  <view class="nearby-content">
    <eui-page-frame :topHeight="100" :withOutBorder="true">
      <template #top>
        <eui-navbar title="附近库存" color="#303133" rightIcon=''></eui-navbar>
      </template>
      <eui-page-content>
        <scroll-view scroll-y class="stock-list">
          <view v-if="loading" class="loading">加载中...</view>
          <view v-else-if="stockList.length === 0" class="empty">暂无附近门店库存信息</view>
          <stock-store-item
              v-for="(item, index) in stockList"
              :key="index"
              :store-item="item"
              background-color="#FFFFFF"
              @handleApply="handleApply"
          >
          </stock-store-item>
        </scroll-view>
      </eui-page-content>
    </eui-page-frame>
  </view>
</template>

<script setup lang="ts">
import {onShow, onLoad} from '@dcloudio/uni-app';
import {ref} from 'vue'
import service from '@/common/service';
import {storeToRefs} from 'pinia';
import {useThemeStore} from '@/store/themeStore';

const themeStore = useThemeStore(); // 主题
const {theme} = storeToRefs(themeStore);
const themeColor = ref(theme._object.theme == 'theme-blue' ? '#02B7E6' : '#FF6430')

import {useMainStore} from "@/store/modules/store";

const mainStore = useMainStore();
const {loginInfo} = mainStore;
import {getRdsUrl, rdsRequest} from "@/hooks/rdsServices";

const rdsUrl = getRdsUrl();

import stockStoreItem from './../components/stockStoreItem.vue';

// 库存列表数据
const stockList = ref<any[]>([]);
const loading = ref(false);

// 引入 useReceiptStore
import {useReceiptStore} from '@/store/useReceiptStore';
const receiptStore = useReceiptStore();
const {cartList} = storeToRefs(receiptStore);

/**
 * 获取附近库存数据
 */
const getData = async () => {
  try {
    loading.value = true;
    uni.showLoading({title: "加载中...", mask: true});

    // 构建 Gcode 参数
    const gcodeParam = cartList.value
      .map(item => `${item.c_gcode}@${item.c_qtty}`)
      .join(',');

    const result = await rdsRequest(
      rdsUrl.apiApi,
      '管易通60RMIS商品调拨',
      'GetGpsStore',
      {
        Gcode: gcodeParam,
        longitude: '112.575654',
        latitude: '37.743084'
      }
    );

    if (result) {
      console.log('result', result)
      stockList.value = result;
    }
  } catch (error) {
    console.error('获取附近库存失败:', error);
    uni.showToast({
      title: '获取数据失败',
      icon: 'none'
    });
  } finally {
    loading.value = false;
    uni.hideLoading();
  }
};

import { useCommonPage } from '../hooks/useCommonPage';
const { checkRouteAndNavigate } = useCommonPage();

const handleApply = (item: any) => {
  try {
    // 保存选中的门店信息到 receiptStore
    receiptStore.updateFromData('md', item);
    
    checkRouteAndNavigate('pages/BusiViews/StoreTransfer/detail');
  } catch (error) {
    console.error('跳转调拨页面失败:', error);
    uni.showToast({
      title: '操作失败，请重试',
      icon: 'none'
    });
  }
};

// 在onLoad中初始化数据
onLoad(async () => {
  await getData();
});

// 页面显示时刷新数据
onShow(async () => {
  await getData();
});
</script>

<style lang="scss" scoped>
.nearby-content {
  ::v-deep .white-main {
    margin: 0;
    border-radius: 30rpx 30rpx 0 0;
  }

  .stock-list {
    width: 100%;
    height: 100%;
    padding: 20rpx;
    box-sizing: border-box;

    .loading,
    .empty {
      text-align: center;
      padding: 40rpx;
      color: #999;
      font-size: 28rpx;
    }
  }
}
</style>